#include <iostream>
#include "../src/keep_alive/ClientAlive.h"
#include "../src/keep_alive/ServerAlive.h"
#include "ListenerRawPlug.h"
#include "../src/NetException.h"
#include "utils.h"

using namespace net;
using namespace rapidxml;

int main(int argc, char* argv[]) {

	ListenerRawPlug listener_raw;
	ClientAlive client_alive;
	ServerAlive server_alive;

	client_alive.subscribe(&listener_raw);
	try {
		server_alive.start(1234);
		client_alive.connectServer("127.0.0.1", 1234);

		std::vector<char> tx, rx;
		tx = stringToVector("Hello!");

		usleep(10*1000);
		rx = server_alive.send(tx);
		std::string msg = vectorToString(rx);
		std::cout << "Returned: " << msg << std::endl;

		client_alive.closeConnection();
		client_alive.connectServer("127.0.0.1", 1234);
		tx = stringToVector("Hello 2!");
		usleep(10*1000);
		rx = server_alive.send(tx);
		msg = vectorToString(rx);
		std::cout << "Returned: " << msg << std::endl;
		client_alive.closeConnection();

		server_alive.stop();

	} catch (NetException &e) {
		std::cout << "NetException: " << e.what() << std::endl;
	}
	std::cout << "Exit 0" << std::endl;
}
